package com.chat.api.web.restctrl;


import cn.hutool.http.HttpUtil;
import com.chat.api.utils.ResultUtil;
import com.chat.api.web.vo.MessageEntity;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;

@RestController
@RequestMapping("/news/")
public class NewsCtrl {

    @Value("${jd.appkey}")
    private String APP_KEY;
    @Value("${jd.new.api}")
    private String NEW_API;
    @Value("${jd.search.new.api}")
    private String SEARCH_NEW_API;
    @Value("${jd.search.driver.question.api}")
    private String DRIVER_QUESTION_API;

    @RequestMapping(value = "getNews",method = RequestMethod.GET)
    @ApiOperation("获取新闻")
    public MessageEntity searchMusic(HttpServletRequest request,
                                     @RequestParam("pageIndex") Integer pageIndex,
                                     @RequestParam("type") String type) throws IOException {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("channel",type);
        paramMap.put("num","5");
        paramMap.put("start",String.valueOf(pageIndex));
        paramMap.put("appkey",APP_KEY);
        String s = HttpUtil.post(NEW_API,paramMap);
        JSONObject data1 = JSONObject.fromObject(s);
        JSONObject data = data1.getJSONObject("result");
        if (!data.getString("msg").equals("ok")){
            return ResultUtil.fail("获取新闻数据失败！");
        }
        JSONObject data2 = data.getJSONObject("result");
        JSONArray list = data2.getJSONArray("list");
        return ResultUtil.success(list);
    }

    @RequestMapping(value = "searchNews",method = RequestMethod.GET)
    @ApiOperation("搜索新闻")
    public MessageEntity searchNews(HttpServletRequest request,
                                     @RequestParam("keyword") String keyword) {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("keyword",keyword);
        paramMap.put("appkey",APP_KEY);
        String s = HttpUtil.post(SEARCH_NEW_API,paramMap);
        JSONObject data1 = JSONObject.fromObject(s);
        JSONObject data = data1.getJSONObject("result");
        if (data == null ||!data.getString("msg").equals("ok")){
            return ResultUtil.fail("获取新闻数据失败！");
        }
        JSONObject data2 = data.getJSONObject("result");
        JSONArray list = data2.getJSONArray("list");
        return ResultUtil.success(list);
    }

    @RequestMapping(value = "getSub",method = RequestMethod.GET)
    @ApiOperation("获取驾照考题")
    public MessageEntity getSub1(HttpServletRequest request,
                                 @RequestParam("type") String type,
                                 @RequestParam("pagenum") String pagenum,
                                 @RequestParam("subject") Integer subject2) {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("type",type);
        paramMap.put("subject",subject2);
        paramMap.put("pagesize",50);
        paramMap.put("pagenum",pagenum);
        paramMap.put("sort","normal");
        paramMap.put("appkey",APP_KEY);
        String s = HttpUtil.post(DRIVER_QUESTION_API,paramMap);
        JSONObject data1 = JSONObject.fromObject(s);
        JSONObject data = data1.getJSONObject("result");
        if (data == null ||!data.getString("msg").equals("ok")){
            return ResultUtil.fail("获取数据失败！");
        }
        JSONObject data2 = data.getJSONObject("result");
        JSONArray list = data2.getJSONArray("list");
//        System.out.println(list);
        return ResultUtil.success(list);
    }


}
